/* 
    arduino
    
    Protospec Home Controller
    
    Copyright (C) 2008  Steven Hepting

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

#include <Wire.h>

uint8_t outbuf[6]; // nunchuck output is 6 bytes of I2C data
uint8_t cnt = 0;
uint8_t ledPin = 13;
uint8_t led_no = 5;
uint8_t state = 0;

// Temperature
uint8_t temperature;
uint8_t temp_pin = 3;
int temp_var;

// Nunchuk return data
uint8_t joy_x_axis;
uint8_t joy_y_axis;
uint16_t accel_x_axis; 
uint16_t accel_y_axis;
uint16_t accel_z_axis;
uint8_t z_button;
uint8_t c_button;

// Loop variables
unsigned long curr_time;
unsigned long next_time = 0;


#define TEMP_WAIT 10000
#define WAIT 50

void setup () {
  beginSerial (115200);
  
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  
  // Set up I2C communication
  Wire.begin ();  // join i2c bus as master (no address needed)
  Wire.beginTransmission (0x52);// transmit to device 0x52
  Wire.send (0x40);		// sends memory address
  Wire.send (0x00);		// sends a zero  
  Wire.endTransmission ();
}

void send_zero () {
  Wire.beginTransmission (0x52);	// transmit to device 0x52
  Wire.send (0x00);		// sends one byte
  Wire.endTransmission ();	// stop transmitting
}

void loop () {
  curr_time = millis();
  Wire.requestFrom (0x52, 6);	// request data from nunchuck
  while (Wire.available()) {
      outbuf[cnt] = (Wire.receive() ^ 0x17) + 0x17;
      cnt++;
    }

  // If we recieved the 6 bytes, then go print them
  if (cnt >= 5) {
      print();
      cnt = 0;
      
      
    }
    
    if (curr_time > next_time){
      Serial.print(curr_time, DEC);
      Serial.print(" ");
      Serial.print(next_time, DEC);
      Serial.print(" ");
      get_temp();
      Serial.print("\r\n");
      next_time = curr_time + TEMP_WAIT;
    }
    
  send_zero (); // send the request for next bytes
  delay(WAIT);
}

// Print the input data we have recieved
void print ()
{
  joy_x_axis = outbuf[0];
  joy_y_axis = outbuf[1];
  accel_x_axis = outbuf[2]; // plus bits 2+3 of 5
  accel_y_axis = outbuf[3]; // plus bits 4+5 of 5
  accel_z_axis = outbuf[4]; // plus bits 6+7 of 5

 // byte outbuf[5] contains bits for z and c buttons
  z_button = outbuf[5] & 0x01;
  c_button = (outbuf[5]  >> 1) & 0x01;

  if (z_button == 1) {
    digitalWrite(led_no, LOW);
  } else {
    digitalWrite(led_no, HIGH);
  }
 
  if (state == 0) {
    if (joy_x_axis < 110) {
      if (led_no > 3) {
        led_no--;
      }
      state = 4;
    } else if (joy_x_axis > 150) {
      if (led_no < 7) {
        led_no++;
      }
      state = 2;
    } else {
      state = 0;
    }
  }
  if ((state == 4) && (joy_x_axis > 120)){
      state = 0;
  }

  if ((state == 2) && (joy_x_axis < 130)){
      state = 0;
  }
  /*
  Serial.print (joy_x_axis, DEC);
  Serial.print (" ");

  Serial.print (joy_y_axis, DEC);
  Serial.print (" ");


  Serial.print (accel_x_axis, DEC);
  Serial.print ("\t");

  Serial.print (accel_y_axis, DEC);
  Serial.print ("\t");

  Serial.print (accel_z_axis, DEC);
  Serial.print ("\t");

  Serial.print (z_button, DEC);
  Serial.print ("\t");

  Serial.print (c_button, DEC);
  Serial.print ("\t");
  
  
  Serial.print (led_no, DEC);
  Serial.print (" ");
  
  Serial.print (state, DEC);
  Serial.print (" ");
                     
  Serial.print ("\r\n");*/
}

void get_temp()
{
  temp_var = analogRead(temp_pin);
  temperature = map(temp_var, 0, 1023, 0, 500);
  Serial.print("t");
  Serial.print(temperature, DEC);
  Serial.print(" ");
}
